package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOPeration{
    @Override
    public void work(BookList booklist) {
        System.out.println("新增图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要新增图书的书名：");
        String name = scanner.nextLine();
        System.out.println("请输入您要新增图书的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入您要新增图书的价格：");
        int price = scanner.nextInt();
        System.out.println("请输入您要新增图书的类型：");
        String type = scanner.next();

        Book book = new Book(name,author,price,type);
        int currentSize = booklist.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book tmp = booklist.getBook(i);
            if(tmp.getName().equals(name)){
                System.out.println("存在这本书，不能重复添加！");
                System.out.println(book);
                return;
            }
        }
        //没有重复的书开始新增
        booklist.setBook(book,currentSize);
        booklist.setUsedSize(currentSize + 1);
    }
}
